package com.lxj.fast_android_demo.dto;

import java.util.HashMap;

import com.lxj.fast_android_demo.util.DatasetUtil;

/**
 * 数据集的列定义对象
 * 
 * @author LiangXiangJu
 * @creationTime 2014-6-30 上午10:57:28
 * @version
 */
public class DataField {
	// 所属数据集
	private Dataset dataset;
	// 列属性名称
	private String name;
	// 数据类型
	private MeapDataType type;
	// 默认值
	private Object defaultValue;

	public DataField(Dataset dataset, String name, MeapDataType type,
			String defaultValue) {
		this.dataset = dataset;
		this.name = name;
		this.type = type;

		Object object = null;
		switch (type) {
		case STRING:
			object = defaultValue;
			break;
		case INT:
			object = DatasetUtil.forceParseInt(defaultValue);
			break;
		case DOUBLE:
			object = DatasetUtil.parseDouble(defaultValue);
			break;
		default:
			break;
		}
		this.defaultValue = object;
	}

	public DataField(Dataset dataset, HashMap<String, String> datas) {
		this.dataset = dataset;
		if (null != datas) {
			if (datas.containsKey(Constant.FIELD_NAME)) {
				this.name = datas.get(Constant.FIELD_NAME);
			}
			if (datas.containsKey(Constant.FIELD_TYPE)) {
				this.type = MeapDataType.getMeapDataTypeByValue(DatasetUtil
						.parseInt(datas.get(Constant.FIELD_TYPE)));
			}
			if (datas.containsKey(Constant.FIELD_DEFAULT)) {
				String defaultValue = datas.get(Constant.FIELD_DEFAULT);
				switch (type) {
				case STRING:
					this.defaultValue = defaultValue;
					break;
				case INT:
					this.defaultValue = DatasetUtil.forceParseInt(defaultValue);
					break;
				case DOUBLE:
					this.defaultValue = DatasetUtil.parseDouble(defaultValue);
					break;
				default:
					break;
				}
			}
		}
	}

	public Dataset getDataset() {
		return dataset;
	}

	public String getName() {
		return name;
	}

	public MeapDataType getType() {
		return type;
	}

	public Object getDefaultValue() {
		return defaultValue;
	}

}
